package com.payment.ktb.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.payment.ktb.R;
import com.payment.ktb.activity.main1.CashierActivity;
import com.payment.ktb.activity.main4.CardWrapActivity;
import com.payment.ktb.model.VouchersEntity;
import com.payment.ktb.utils.ToastUtils;

import java.util.List;

/**
 * Created by admin on 2017/9/6.
 */

public class CardWrapAdapter extends BaseAdapter {


    Context mContext;
    List<VouchersEntity> listData;

    public CardWrapAdapter(Context mContext, List<VouchersEntity> listData) {
        this.mContext = mContext;
        this.listData = listData;
    }

    public CardWrapAdapter() {

    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int i) {
        return listData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder viewHolder = null;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = View.inflate(mContext, R.layout.adapter_cardwrap_item,null);
            viewHolder.tv_cardwrap_validdate = (TextView) view.findViewById(R.id.tv_cardwrap_validdate);
            viewHolder.tv_cardwrap_name = (TextView) view.findViewById(R.id.tv_cardwrap_name);
            viewHolder.iv_cardwrap_2yuan_valid = (ImageView) view.findViewById(R.id.iv_cardwrap_2yuan_valid);
            viewHolder.iv_cardwrap_status = (ImageView) view.findViewById(R.id.iv_cardwrap_status);
            viewHolder.btn_cardwrap_confirm = (Button) view.findViewById(R.id.btn_cardwrap_confirm);
            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();
        }


        viewHolder.tv_cardwrap_name.setText(listData.get(i).getVouchersName());
        viewHolder.tv_cardwrap_validdate.setText("有效期至" + listData.get(i).getExpireTime());




        if (listData.get(i).getVouchersStatus() == 0) {
            viewHolder.iv_cardwrap_2yuan_valid.setImageResource(R.mipmap.cardwrap_2yuan_valid);
            viewHolder.iv_cardwrap_status.setVisibility(View.INVISIBLE);
            viewHolder.btn_cardwrap_confirm.setEnabled(true);
        }else if (listData.get(i).getVouchersStatus() == 9){
            //expire
            viewHolder.iv_cardwrap_2yuan_valid.setImageResource(R.mipmap.cardwrap_2yuan_invalid);
            viewHolder.iv_cardwrap_status.setImageResource(R.mipmap.cardwrap_expired);
            viewHolder.btn_cardwrap_confirm.setEnabled(false);
        } else {
            //已使用
            viewHolder.iv_cardwrap_2yuan_valid.setImageResource(R.mipmap.cardwrap_2yuan_invalid);
            viewHolder.iv_cardwrap_status.setImageResource(R.mipmap.cardwrap_used);
            viewHolder.btn_cardwrap_confirm.setEnabled(false);
        }

        viewHolder.btn_cardwrap_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mContext.startActivity(new Intent(mContext,CashierActivity.class));
            }
        });

    return view;

    }

    class ViewHolder {
        ImageView iv_cardwrap_2yuan_valid;
        TextView tv_cardwrap_validdate;
        ImageView iv_cardwrap_status;
        Button btn_cardwrap_confirm;
        TextView tv_cardwrap_name;
    }


}
